//
//  XiaoQiController.m
//  SleepingElf
//
//  Created by karong on 2023/11/2.
//

#import "XiaoQiController.h"
#import "UIButton+ImageAlign.h"

@interface XiaoQiController ()
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
@property (weak, nonatomic) IBOutlet UILabel *defaultViewWakeUpTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *runViewWakeUpTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *wakeUpViewWakeUpTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *wakeUpViewNextTimeLabel;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIView *defaultView;
@property (weak, nonatomic) IBOutlet UIView *runTimeView;
@property (weak, nonatomic) IBOutlet UIView *wakeUpView;
@property (weak, nonatomic) IBOutlet UILabel *runTimeLabel;
@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, assign) NSInteger timeState;
@end

@implementation XiaoQiController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.startBtn setButtonImageTitleStyle:(GRButtonImageTitleStyleLeft) padding:4];
    self.selectIndex = 14;
    [self.pickerView selectRow:self.selectIndex inComponent:0 animated:NO];
    [self layoutWakeUpTime];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.pickerView.subviews[1].backgroundColor = UIColor.clearColor;
    });
    [self addObserver];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keepTimeChangeNotification:) name:SEKeepTimeChangeNotification object:nil];
}

- (void)keepTimeChangeNotification:(NSNotification*)notification {
    NSInteger time = [[notification object] integerValue];
    if (time > 0) {
        self.timeState = 1;
        self.runTimeLabel.text = [self getMMSSFromSS:time];
    }
    else if (time == 0) {
        self.timeState = 2;
    }
}

// 毫秒转时分秒
-(NSString *)getMMSSFromSS:(NSInteger)totalTime{
    
    NSInteger seconds = totalTime;
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds/60)];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    NSString *format_time = @"";
    format_time = SESF(@"%@:%@",str_minute,str_second);
    
    return format_time;
}

- (void)layoutWakeUpTime {
    self.defaultViewWakeUpTimeLabel.text = SESF(WSLocalized(@"将在%@唤醒你"), [self afterTime:(self.selectIndex + 1)]);
    self.runViewWakeUpTimeLabel.text = SESF(WSLocalized(@"将在%@唤醒你"), [self afterTime:(self.selectIndex + 1)]);
    self.wakeUpViewWakeUpTimeLabel.text = SESF(WSLocalized(@"将在%@唤醒你"), [self afterTime:(self.selectIndex + 1)]);
    self.wakeUpViewNextTimeLabel.text = SESF(WSLocalized(@"将在%@唤醒你"), [self afterTime:(self.selectIndex + 1 + 5)]);

}

- (NSString *)afterTime:(NSInteger)minie {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    NSDate *nowDate = [NSDate date];
    // n秒后时间
    NSDate *afterDate = [NSDate dateWithTimeInterval:minie*60 sinceDate:nowDate];
    NSString *afterTime = [formatter stringFromDate:afterDate];
    return afterTime;
}

- (void)layoutDefaultView {
    self.defaultView.hidden = NO;
    self.runTimeView.hidden = YES;
    self.wakeUpView.hidden = YES;
}

- (void)layoutRunTimeView {
    self.defaultView.hidden = YES;
    self.runTimeView.hidden = NO;
    self.wakeUpView.hidden = YES;
}

- (void)layoutWakeUpView {
    self.defaultView.hidden = YES;
    self.runTimeView.hidden = YES;
    self.wakeUpView.hidden = NO;
}

- (IBAction)closeAction:(id)sender {
    if (appConfig.wakeUpType == 1) {
        [self stopAction:nil];
    }
}

- (IBAction)startAction:(id)sender {
    self.timeState = 1;
    appConfig.wakeUpType = 1;
    appConfig.xiaoQiConfig.time = (self.selectIndex + 1) * 60;
    [appConfig.xiaoQiConfig starTimeKeeper];
}

- (IBAction)moreTimeAction:(id)sender {
    self.selectIndex = 4;
    [self layoutWakeUpTime];
    self.timeState= 1;
    appConfig.wakeUpType = 1;
    [appConfig defaultTimeKeeper];
    appConfig.xiaoQiConfig.time = 5 * 60;
    [appConfig.xiaoQiConfig starTimeKeeper];
}


- (IBAction)stopAction:(id)sender {
    self.timeState = 0;
    appConfig.wakeUpType = 0;
    [appConfig defaultTimeKeeper];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 15;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIColor *  color = [UIColor colorFromHexRGB:@"ffffff"];
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = 1;
    label.font = [UIFont systemFontOfSize:50 weight:(UIFontWeightMedium)];
    label.textColor = color;
    label.text = SESF(@"%ld", (long)row + 1);
    return label;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 72;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectIndex = row;
    [self layoutWakeUpTime];
}

- (void)setTimeState:(NSInteger)timeState {
    _timeState = timeState;
    switch (timeState) {
        case 0:
            [self layoutDefaultView];
            break;
        case 1:
            [self layoutRunTimeView];
            break;
        case 2:
            [self layoutWakeUpView];
            break;
        default:
            break;
    }
}
@end
